package com.puboot.module.admin.controller;

import com.puboot.common.util.IdcardValidatorUtil;
import com.puboot.common.util.ResultUtil;
import com.puboot.module.admin.model.*;
import com.puboot.module.admin.service.BizCoursePlanService;
import com.puboot.module.admin.service.BizCourseService;
import com.puboot.module.admin.service.BizTeacherService;
import com.puboot.module.admin.service.BizTeachingService;
import com.puboot.module.admin.vo.base.ResponseVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 前端接口
 * @author ltc
 * @version 1.0
 * @date 2020/10/9 10:54
 */
@CrossOrigin
@Controller
@RequestMapping("/front")
@AllArgsConstructor
public class FrontWebController {

    //课程
    private final BizCourseService bizCourseService;
    private final BizCoursePlanService bIzCoursePlanService;
    //用户报名授课
    private final BizTeachingService bizTeachingService;

    private final BizTeacherService bizTeacherService;

    //校验身份证
    @PostMapping("/idcard")
    @ResponseBody
    @CrossOrigin
    public ResponseVo isValidatedAllIdcard(@RequestBody IdcardVali idcard) {
        boolean validatedAllIdcard = IdcardValidatorUtil.isValidatedAllIdcard(idcard.getIdcard());
        String str=idcard.getIdcard();
        if (validatedAllIdcard)
            return ResultUtil.success("身份证符合要求");
        return ResultUtil.error("身份证不符合要求");
    }
    //查询课程列表
    @GetMapping("listAll")
    @ResponseBody
    @CrossOrigin
    public ResponseVo loadListCourse() {
        List<BizCourse> bizCourses = bizCourseService.list();
        return ResultUtil.success("查询所有课程", bizCourses);
    }

    //根据课程ID查询可曾安排表
    @GetMapping("/plan/{courseId}")
    @ResponseBody
    @CrossOrigin
    public ResponseVo getBIzCoursePlanByCourseId(@PathVariable Integer courseId) {
        List<BizCoursePlan> byCourseId = bIzCoursePlanService.getByCourseId(courseId);
        return ResultUtil.success("根据课程ID查询课程安排", byCourseId);
    }
    //推荐老师
    @GetMapping("/teacher")
    @ResponseBody
    @CrossOrigin
    public ResponseVo listTeachers(){
        List<BizTeacher> list = bizTeacherService.list();
        return ResultUtil.success("查询所有老师", list);
    }

    //保存学生报名信息
    @PostMapping("/addInfo")
    @ResponseBody
    @CrossOrigin
    public ResponseVo add(@RequestBody BizTeaching bizTeaching) {
        bizTeaching.setStatus(1);
         Date date = new Date();
        bizTeaching.setCreateTime(date);
        bizTeaching.setUpdateTime(date);

        if (null !=bizTeaching.getTtype()){
            bizTeaching.setTtype(1);
        }


        Integer sort = bizTeachingService.maxSort();
        bizTeaching.setSort(sort+1);
        boolean i = bizTeachingService.save(bizTeaching);
        if (i){
            BizTeaching byId = bizTeachingService.getById(bizTeaching.getId());
            BizCoursePlan byCourseId = bIzCoursePlanService.getById(byId.getCoursePlanId());
            byId.setCoursePlans(byCourseId);
            BizCourse course = bizCourseService.getById(bizTeaching.getCourseId());
            byId.setCourses(course);
            BizTeacher teacher = bizTeacherService.getById(bizTeaching.getTeacherId());
            byId.setTeachers(teacher);
            return ResultUtil.success("新增报名授课信息成功",byId);

        }

        return ResultUtil.error("新增报名授课信息失败");
    }

}
